package com.jhws.sys.cms.api;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import com.jhws.business.aliyun.service.OSSClientService;
import com.jhws.common.base.bo.Global;
import com.jhws.util.LogHelper;
import com.jhws.util.ObjectUtils;
import com.jhws.util.StringUtils;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Maps;
import com.jhws.common.base.bo.JsonRespData;
import com.jhws.sys.cms.service.ResourceService;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;

/**
 * cms接口
 *
 * @author xiaokek
 * @since 2016年12月18日 下午7:27:46
 */
@Controller
@RequestMapping("/cms")
public class CmsApi {

    @Resource
    private ResourceService rs;
    @Resource
    private OSSClientService ossClientService;

    @SuppressWarnings("unchecked")
    @RequestMapping({"queryResources"})
    @ResponseBody
    @ApiOperation(value = "查询内容资源信息", httpMethod = "POST", response = JsonRespData.class, notes = "查询内容资源信息")
    public JsonRespData queryResources(
            @ApiParam(required = true, name = "typeCode", value = "分类编码")
            @RequestParam("typeCode") String typeCode) throws IOException {

        Map param = Maps.newHashMap();
        param.put("type_code", typeCode);
        param.put("isApproved", Global.CMS_APPROVED);
        Map<String, Object> result = rs.queryResourcePage(true, param);
        return new JsonRespData("queryResources", 1, result);
    }

    @RequestMapping({"getHomepageContent"})
    @ResponseBody
    public JsonRespData getHomepageContent(String commCode) throws IOException {
        String typeCode = "comm_" + commCode + "_" + "CarouselAdvertising";
        List<Map<String, Object>> carouselAdvertising = rs.queryResourceList(false, typeCode, null, null);
        if (carouselAdvertising.size() == 0) {
            typeCode = "comm_0000_CarouselAdvertising";
            carouselAdvertising = rs.queryResourceList(false, typeCode, null, null);
        }

        typeCode = "comm_" + commCode + "_" + "FunctionButton";
        List<Map<String, Object>> functionButton = rs.queryResourceList(false, typeCode, null, null);
        if (functionButton.size() == 0) {
            typeCode = "comm_0000_FunctionButton";
            functionButton = rs.queryResourceList(false, typeCode, null, null);
        }
        HashedMap hashedMap = new HashedMap();
        hashedMap.put("carouselAdvertising", carouselAdvertising);
        hashedMap.put("functionButton", functionButton);
        return new JsonRespData("getHomepageContent", 1, hashedMap);
    }

    /**
     * 门口机图片广告
     *
     * @param commCode 小区编号
     * @param date     日期
     * @return
     * @throws IOException
     */
    @RequestMapping("getOutdoorAdvertising")
    @ResponseBody
    public JsonRespData getOutdoorAdvertising(String commCode, String date, String screenType) throws IOException {
        LogHelper.d("------------->    " + screenType);
        String where = "where 1=1 and comm_code ='" + commCode
                + "' and type_code='outdoor_image_ads' ";
        if (StringUtils.isNotEmpty(screenType)) {
            where += " and customType='" + screenType + "'";
        } else {
            where += " and (customType='cross' or customType='NULL')";
        }
        //增量查询
        if (StringUtils.isNotEmpty(date)) {
            where += " and create_time > '" + date + "'";
        }
        List<com.jhws.sys.cms.bo.Resource> outdoorAdvertising = rs.queryResourceList(where);
        for (com.jhws.sys.cms.bo.Resource resource : outdoorAdvertising) {
            resource.setIcon_url(ossClientService.getUrl(resource.getIcon_url(), false));
        }
        return new JsonRespData("getOutdoorAdvertising", 1, outdoorAdvertising);
    }

    /**
     * 门口机视频广告
     *
     * @param commCode 小区编号
     * @param date     日期
     * @return
     * @throws IOException
     */
    @RequestMapping("getOutdoorVideoAds")
    @ResponseBody
    public JsonRespData getOutdoorVideoAds(String commCode, String date) throws IOException {
        String where = "where 1=1 and comm_code = '" + commCode + "' " + "and type_code = 'video_ads' ";
        if (!StringUtils.isEmpty(date)) {
            where += " and create_time > '" + date + "'";
        }
        List<com.jhws.sys.cms.bo.Resource> resourceList = rs.queryResourceList(where);
        return new JsonRespData("getOutdoorVideoAds", 1, resourceList);
    }
}
